/*
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

// Painting application in C (no Lua) to use for performance comparison.

#include <maapi.h>
#include <MAUtil/Geometry.h>
//#include <conprint.h>
//#include "MAHeaders.h"

static int GetScreenWidth()
{
	MAExtent screenSize = maGetScrSize();
	return EXTENT_X(screenSize);
}

static int GetScreenHeight()
{
	MAExtent screenSize = maGetScrSize();
	return EXTENT_Y(screenSize);
}

static void SetColor(int red, int green, int blue)
{
	int color = (red << 16) | (green << 8) | blue;
	maSetColor(color);
}

static void FillRect(int left, int top, int width, int height)
{
	maFillRect(left, top, width, height);
	maUpdateScreen();
}

static void PaintDot(int x, int y)
{
	SetColor(0, 200, 0);
	FillRect(x - 5, y - 5, 10, 10);
}

static void DrawBackground()
{
	SetColor(255, 255, 255);
	FillRect(0, 0, GetScreenWidth(), GetScreenHeight());
}

void PaintAppInC()
{
	MAEvent event;

	DrawBackground();

	while (1)
	{
		maWait(0);
		while (maGetEvent(&event))
		{
			switch (event.type)
			{
				case EVENT_TYPE_CLOSE:
					return;
				case EVENT_TYPE_POINTER_PRESSED:
				case EVENT_TYPE_POINTER_DRAGGED:
					PaintDot(event.point.x, event.point.y);
					break;
				case EVENT_TYPE_POINTER_RELEASED:
					break;
			}
		}
	}
}
